<?php
if(!defined('ADMIN')){
    exit('Access Denied');
}


?>
<div class="pageHeader">
    <p><b>菜品发布 > 编辑产品</b></p>
</div>
<div class="pageContent" >
    <form method="post" action="index.php?mod=buy&act=ding" class="pageForm required-validate" onsubmit="return validateCallback(this,navTabAjaxDone)">
        <input type="hidden" name="sub" value="save" />
        <input type="hidden" name="id" value="<?=$chefid?>" />
        <div class="pageFormContent nowrap" layoutH="80" style="overFlow-x: hidden;">


            <dl>
                <dt>名厨/名店：</dt>
                <dd>
                    <span style="width: 300px"><?php echo $chef['name']?></span>
                    <span class="pCount" style="float: right">已点<l style="color:red" id="totalNums" nums="0">0</l>个</span>
                    <span style="float: right"><i>共计金额：</i><b class="duiqi" id="totalPrice">0</b><b class="duiqi">元</b></span>


                </dd>
            </dl>
            <dl>
                <dt>选择档期：</dt>
                <dd>
                    <a title="排期" target="dialog" href="<?=u(MOD,'editcheftime',array('id'=>$chefid))?>" width="650" height="500" id="dangqi">排期</a>
                </dd>
            </dl>


            <dl>
                <dt>套餐选择：</dt>
                <dd>
                    <table style="border-collapse:collapse;">
                        <?php
                            foreach($genius_list as $key=>$val)
                            {
                                echo "<tr style=\"border-bottom:1px solid black\"><td><a href=\"javascript:;\" onclick=\"showTao(".$key.");\">".$val['title']."(".$val['totalprice'].")</a>

                                <td>
                                <td><section class=\"bbox\" onclick=\"changeCount(this, event, ".$val['id'].",2);\">
                                    <input type=\"button\" value=\"-\" class=\"btn-reduce\">
                                     <input type=\"text\" readonly=\"readonly\" price=\"".$val['totalprice']."\" value=\"0\" name=\"numBox\" style=\"width:10px;\">
                                     <input type=\"button\" value=\"+\" class=\"btn-plus\">                    </section>
                                </td>
                                </tr>
                                <tr><td><div id=\"tao".$key."\" style=\"display:none\">";
                                foreach ($val['dishes']['id'] as $k=>$v)
                                {
                                    echo "<p>".$val['dishes']['title'][$k]."</p>";
                                }
                                echo "</div></td><td></td></tr>";
                            }
                        ?>
                    </table>
                </dd>
            </dl>
            <dl>
                <dt>自选套餐：</dt>
                <dd style="width: 100%">
                    <table >
                        <tr>
                    <?php
                        //pp($goodsnew);
                        foreach($goodsnew as $gtype=>$val){
                            echo "<td style='vertical-align:top;width: 200px;'><table  style=\"border-collapse:collapse;margin-right:5px\">";
                            echo "<tr style=\"background:#ccc\"><td style=\"color:red;\">".$goodst[$gtype]."</td></tr>";
                                foreach($val as $v)
                                {
                                    echo "<tr style=\"border-bottom:1px solid black\"><td height=\"25px\" >
                                        <a href=\"".u(MOD,'remark',array('id'=>$v['id']))."\" width=\"500\" height=\"400\" target=\"dialog\">".$v['title']."(".$v['productprice'].")</a>
                                    </td><td >
                                    <section class=\"bbox\" onclick=\"changeCount(this, event, ".$v['id'].",1);\">
                                    <input type=\"button\" value=\"-\" class=\"btn-reduce\">
                                     <input type=\"text\" readonly=\"readonly\" price=\"".$v['productprice']."\" value=\"0\" name=\"numBox\" style=\"width:10px;\">
                                     <input type=\"button\" value=\"+\" class=\"btn-plus\">                    </section>
                                    </td></tr>";
                                }
                            echo "</table></td>";
                        }

                    ?>
                        </tr>
                    </table>
                </dd>
            </dl>
            <dl>
                <dt>餐具：</dt>
                <dd>
                    <input name="dishware" id="dishware" type="text" value="0" onblur="countTotal()">(每套<?=$dishware?>元)
                </dd>
            </dl>
            <dl>
                <dt>服务员：</dt>
                <dd>
                    <input name="waiter" id="waiter" type="text" value="0" onblur="countTotal()">(每套<?=$waiter?>元)
                </dd>
            </dl>

            <dl>
                <dt>您的姓名：</dt>
                <dd>
                    <input type="text" class="required textInput error"  style="width:200px;" maxlength="30" placeholder="(必填*)请输入您的真是姓名" value="" id="guest_name" name="guest_name">
                </dd>
            </dl>
            <dl>
                <dt>手机号吗：</dt>
                <dd>
                    <input type="tel" class="required phone error" style="width:200px;" placeholder="(必填*)请输入您的手机号码" maxlength="20" value="" name="tel" id="tel">
                </dd>
            </dl>
            <dl>
                <dt>详细地址：</dt>
                <dd>
                    <input type="text" id="text" name="address" value="" placeholder="请输入您的详细地址" class="input_text textInput" style="width: 400px;">
                </dd>
            </dl>
            <dl>
                <dt>备注说明：</dt>
                <dd>
                    <textarea class="input_text" placeholder="如有附加需求或忌口，请备注,我们会尽量安排" maxlength="200" style="width:300px;height:60px;" name="remark"></textarea>
                </dd>
            </dl>
            <dl>
                <dt>优惠券使用：</dt>
                <dd>
                    <input type="text" class="input_text" style="width:200px;" maxlength="30" placeholder="请输入优惠劵" value="" id="coupon" name="coupon"> <a href="javascript:;" onclick="checkCoupon()">检查优惠劵</a><span id="couponHtml"></span>
                </dd>
            </dl>
        </div>
        <div class="formBar">
            <ul style="float:left;">
                <li><div class="buttonActive"><div class="buttonContent"><button type="submit">确定下单</button></div></div></li>
            </ul>
        </div>
        <input type="hidden" id="orderPrice" name="orderPrice" value="0">
        <input type="hidden" id="couponPrice" name="couponPrice" value="0">
        <input type="hidden" id="orderDate" name="orderDate" value="0">
        <input type="hidden" id="orderRemark" name="orderRemark" value="">

    </form>

    <script type="text/javascript">
        var totalNums = 0;
        var totalPrice = 0;
        var goodsArr  = new Array();//订餐数组，包括单点
        var taoArr = new Array();//套餐数组
        var remarkArr = new Array();//备注数组
        var remarkStr = '';//备注字符串


        function changeCount(thi, evt, dishes_id,type) {
            //alert(dishes_id);
            if ("button" == evt.target.type) {
                var counter = thi.querySelectorAll("input[name='numBox']")[0];//当前菜的数量
                var val = parseInt(counter.value);//当前菜的数量

                var totalNums = parseInt($("#totalNums").html());//总数量
                if ("btn-reduce" == evt.target.className) {
                    val--;
                } else {
                    val++;
                }
                if(val >= 0){
                    if(type == 1)
                    {
                        //修改当前的goodsArr数组,包含套餐
                        if(!goodsArr.hasOwnProperty(dishes_id))
                        {
                            //alert(123);
                            var price =  parseInt(counter.attributes["price"].nodeValue);
                            goodsArr[dishes_id] = new Array();
                            goodsArr[dishes_id]['id'] = dishes_id;
                            goodsArr[dishes_id]['price'] = price;
                        }
                        goodsArr[dishes_id]['num'] = val;
                    }
                    else{
                        if(!taoArr.hasOwnProperty(dishes_id))
                        {
                            //alert(123);
                            var price =  parseInt(counter.attributes["price"].nodeValue);
                            taoArr[dishes_id] = new Array();
                            taoArr[dishes_id]['id'] = dishes_id;
                            taoArr[dishes_id]['price'] = price;
                        }
                        taoArr[dishes_id]['num'] = val;
                    }

                    if ("btn-reduce" == evt.target.className) {
                        totalNums = totalNums - 1;
                    } else {
                        totalNums = totalNums + 1;
                    }
                    
                    counter.value = Math.max(0, val);//当前菜的数量

                    //菜的总数量
                    $("#totalNums").html(totalNums);
                    //计算菜的总价格
                    countTotal();





                }else{
                    alert('数量已经小于0');

                }


                //switchMenu();
            }
        }

function showTao(key)
{
    if($('#tao'+key).css('display') == 'block')
    {
        $('#tao'+key).hide();
    }
    else{
        $('#tao'+key).show();
    }
}

function checkCoupon()
{
    var code = $('#coupon').val();
    if(code == '')
    {
        alert('请输入优惠劵');
        return false;
    }
    var jine = $("#totalPrice").html();
    $.post("<?php echo u('buy','checkcoupon')?>", { code:code,jine:jine},
        function(data){
            if(data == -1)
            {
                alert('请输入优惠码');
            }
            if(data == -5)
            {
                alert('金额不能为空');
            }
            if(data == -2)
            {
                alert('无此优惠劵');
            }
            if(data == -3)
            {
                alert('已使用过');
            }
            if(data == -4)
            {
                alert('已过期');
            }
            if(data == -6)
            {
                alert('消费金额不够');
            }
            if(data > 0)
            {
                //alert('优惠劵有效');
                $("#couponPrice").val(data);
                $("#couponHtml").html(data);
                countTotal();
            }

        });

}

//计算总金额
function countTotal()
{
    //菜的总价格
    totalPrice = 0;
    for(key in goodsArr){

        totalPrice += parseFloat(goodsArr[key]['price'])*parseInt(goodsArr[key]['num']);
    }
    for(key in taoArr){

        totalPrice += parseFloat(taoArr[key]['price'])*parseInt(taoArr[key]['num']);
    }
    //餐具服务员
    var dishware = $('#dishware').val();
    var waiter = $('#waiter').val();
    totalPrice += dishware*<?=$dishware;?>;
    totalPrice += waiter*<?=$waiter;?>;
    var couponPrice = $('#couponPrice').val();
    //alert(couponPrice);
    if(couponPrice > 0)
    {
        totalPrice -= couponPrice;
    }
    $("#totalPrice").html(totalPrice);
    $("#orderPrice").val(totalPrice);
    //生成要提交的菜的信息
    var goodStr = arrayToJson(goodsArr);
    var taoStr = arrayToJson(taoArr);
    $("#orderGoods").val(goodStr);
    $("#orderTao").val(taoStr);

}

function arrayToJson(o) {
    //alert(typeof o);
    var r = [];
    if (typeof o == "string") return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";

    if (typeof o == "object") {
        if (!o.sort) {
            alert(o.sort);
            for (var i in o)
                r.push(i + ":" + arrayToJson(o[i]));
            if (!!document.all && !/^\n?function\s*toString\s*\{\n?\s*\[native code\]\n?\s*\}\n?\s*$/.test(o.toString)) {
                r.push("toString:" + o.toString.toString());
            }
            r = "{" + r.join() + "}";
        } else {
            //alert(123);
            for (key in o) {
                //alert(key);
                //alert(o.length);
                r.push(arrayToJson(o[key]));
            }
            r = "[" + r.join() + "]";
        }
        return r;
    }
    return o.toString();
}
</script>